# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

"""HUD module"""
from __future__ import division, absolute_import
import pyglet
from src import views
from src import game
from src import inp
from src import vars

win = game.win

def draw_rect(x, y, w, h, type=pyglet.gl.GL_LINE_LOOP):
    x = (x + game.vp.x_shift) % game.field.w_px # FIXME
    y = (y + game.vp.y_shift) % game.field.h_px
    pyglet.graphics.draw(4, type,
                         ('v2f', (x, y, x+w, y, x+w, y+h, x, y+h)),
                         ('c4B', (255, 255, 255, 255) * 4))
@views.has_init_view
class HUD(object):
    def __init__(self):
        self.sel_objects = []
        self.sel_units = []
        self.selection_rect = [0, 0, 0, 0]
        self.selection = False
        self.panning = False
        self.show_fps = False
        self.init_view()
        #pyglet.event.EventDispatcher.__init__(self)
    def init_view(self):
        self.widgets = []
        self.top_status = pyglet.text.Label('Top Status',
                                            font_size = 18,
                                            y = win.height - 20,
                                            x = 10)
        self.widgets.append(self.top_status)
        self.fps_display = pyglet.window.FPSDisplay(win)
        p = self.fps_display.label
        p.x, p.y = win.width - 2, win.height - 12
        p.font_size = 9 
        p.anchor_x='right'
        p.color = (224,0,0,255)
        #self.push_handlers(self)
    def __setstate__(self, state):
        self.__dict__.update(state)
        self.init_view()
    def toggle_fps_display(self):
        self.show_fps = not self.show_fps
    def drag_start(self, x, y, buttons):
        print 'BUTTONS', buttons
        self.hpx = (inp.m.x - gamp.vp.x_shift) % game.field.w_px # mx to field.x
        self.hpy = (inp.m.y - game.vp.y_shift) % game.field.h_px
        print 'HPX', self.hpx
        if buttons == 4:
            self.selection = False
            self.panning = True
        else:
            self.selection_rect[0:2] = [x, y]
            self.selection = True
    def drag_update(self, x, y):
        mul = 1
        if self.selection:
            x1, y1 = self.selection_rect[0:2]
            self.selection_rect[2:4] = [-(x1-x), -(y1-y)]
        else:
            dx = self.hpx - x
            print 'DX', dx
            game.vp.x = (self.hpx - x) * mul
            game.vp.y = (self.hpy - y) * mul
    def drag_end(self, x, y):
        if self.panning:
            self.panning = False
            return
        self.selection_rect = [0, 0, 0, 0]
        self.selection = False
    def draw(self):
        for w in self.widgets:
            w.draw()
        if self.selection:
            draw_rect(*self.selection_rect)
        if self.show_fps:
            self.fps_display.draw()
        for u in game.unitctl.get_selected_units():
            draw_rect(u.x, u.y, vars.tile_size, vars.tile_size) 
    def update(self):
        x = inp.m.x
        y = inp.m.y
        if self.selection:
            print '>>> sel update'
            x1, y1 = self.selection_rect[0:2]
            self.selection_rect[2:4] = [-(x1-x), -(y1-y)]
            game.ctl.dispatch_event('on_hud_selection_update', self.selection_rect)
        #print '-- sel:', self.selection_rect, x, y
    def selection_start(self):
        self.selection = True
        self.selection_rect[0:2] = [inp.m.x, inp.m.y]
        print '>>> sel start'
    def selection_done(self):
        self.selection = False
        print '>>> sel done'
    def on_mouse_motion(self, x, y, dx, dy):
        #print '-----> hello'
        #self.dispatch.event('on_mouse_motion')
        self.top_status.text = "%d %d (%d %d) z:%d" \
            % (x, y, inp.m.cx, inp.m.cy, game.field.zlevel[inp.m.cx][inp.m.cy])
        self.update()
    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if buttons == 1:
            if not self.selection:
                self.selection_start()
        elif buttons == 4:
            if not self.panning:
                self.panning_start()
        print '+++++++hud on mouse drug'
    def on_mouse_release(self, x, y, buttons, modifiers):
        if buttons == 1:
            if self.selection:
                self.selection_done()
            else:
                game.ctl.dispatch_event('on_hud_click', x, y, buttons, modifiers)
        elif buttons == 2:
            if self.selection:
                game.unitctl.unselect_all()
        

from src.gamectl import GameController
#GameController.register_event_type('on_hud_selection_start')
GameController.register_event_type('on_hud_selection_update')
#GameController.register_event_type('on_hud_selection_done')
GameController.register_event_type('on_hud_click')

"""

HUD do not controll anything really, use controllers

# registering events
for event_type in pyglet.window.Window.event_types:
    print 'registered', event_type
    HUD.register_event_type(event_type)
"""
